// -----------------------------------------------------------------------
// <copyright file="DepartmentEN.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Class Department
    /// </summary>
    public class DepartmentEN : EntityEN
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DepartmentEN" /> class
        /// </summary>
        public DepartmentEN()
        {
            this.DepartmentId = 0;
            this.Code = string.Empty;
            this.Name = string.Empty;
            this.Parent = 0;
            this.Order = 0;
        }

        /// <summary>
        /// Gets or sets DepartmentId
        /// </summary>
        public int DepartmentId { get; set; }

        /// <summary>
        /// Gets or sets Code
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// Gets or sets Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets Parent
        /// </summary>
        public int Parent { get; set; }

        /// <summary>
        /// Gets or sets Order
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        /// Gets NodeWorkflowRolls
        /// </summary>
        public IList<NodeWorkflowRollEN> NodeWorkflowRolls
        {
            get
            {
                try
                {
                    IList<NodeWorkflowRollEN> lstNodeWorkflowRolls = new List<NodeWorkflowRollEN>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetByDepartmentId, this.DepartmentId);
                    while (reader.Read())
                    {
                        NodeWorkflowRollEN nodeWorkflowRoll = new NodeWorkflowRollEN();
                        if (!reader.IsDBNull(0))
                        {
                            nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            nodeWorkflowRoll.RoleName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                        }

                        lstNodeWorkflowRolls.Add(nodeWorkflowRoll);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstNodeWorkflowRolls;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets UserDepartments
        /// </summary>
        public IList<UserDepartmentEN> UserDepartments
        {
            get
            {
                try
                {
                    IList<UserDepartmentEN> lstUserDepartment = new List<UserDepartmentEN>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDepartmentGetByDepartmentId, this.DepartmentId);
                    while (reader.Read())
                    {
                        UserDepartmentEN userDepartment = new UserDepartmentEN();
                        if (!reader.IsDBNull(0))
                        {
                            userDepartment.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            userDepartment.UserId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            userDepartment.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            userDepartment.PositionsId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            userDepartment.Active = reader.GetBoolean(4);
                        }

                        lstUserDepartment.Add(userDepartment);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstUserDepartment;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets the department parent.
        /// </summary>
        public DepartmentEN DepartmentParent
        {
            get
            {
                try
                {
                    DepartmentEN department = null;
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDepartmentGetById, this.Parent);
                    while (reader.Read())
                    {
                        department = new DepartmentEN();

                        if (!reader.IsDBNull(0))
                        {
                            department.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            department.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            department.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            department.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            department.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                    return department;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
